package cn.edu.bistu.accesswordsapp;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String tag = "MyWordTag";
    private ContentResolver contentResolver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contentResolver = this.getContentResolver();

        //得到按钮
        Button all = findViewById(R.id.btn_all);
        Button insert = findViewById(R.id.btn_insert);
        Button delete = findViewById(R.id.btn_delete);
        Button allDelete = findViewById(R.id.btn_allDelete);
        Button update = findViewById(R.id.btn_update);
        Button query = findViewById(R.id.btn_query);


        //获取全部
        all.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                Cursor cursor = contentResolver.query(Words.Word.CONTENT_URI,
                        new String[]{Words.Word._ID, Words.Word.COLUMN_WORD, Words.Word.COLUMN_MEANING, Words.Word.COLUMN_SAMPLE},
                        null, null, null);

                if (cursor == null) {
                    Toast.makeText(MainActivity.this, "没有找到记录", Toast.LENGTH_LONG).show();
                    return;
                }
                String msg = "";
                if (cursor.moveToNext()) {
                    do {
                        msg += "ID：" + cursor.getInt(cursor.getColumnIndex(Words.Word._ID)) + ",";
                        msg += "单词：" + cursor.getString(cursor.getColumnIndex(Words.Word.COLUMN_WORD)) + ",";
                        msg += "含义：" + cursor.getInt(cursor.getColumnIndex(Words.Word.COLUMN_MEANING)) + ",";
                        msg += "例子：" + cursor.getFloat(cursor.getColumnIndex(Words.Word.COLUMN_SAMPLE)) + "\n";
                    } while (cursor.moveToNext());


                }
                Log.v(tag, msg);


            }
        });

        //增加
        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String strWord = "branch";
                String strMeaning = "branch";
                String strSample = "This is a branch.";
                ContentValues values = new ContentValues();

                values.put(Words.Word.COLUMN_WORD, strWord);
                values.put(Words.Word.COLUMN_MEANING, strMeaning);
                values.put(Words.Word.COLUMN_SAMPLE, strSample);

                Uri newUri = contentResolver.insert(Words.Word.CONTENT_URI, values);

            }
        });

        //删除
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = "6";
                Uri uri = Uri.parse(Words.Word.CONTENT_URI_STRING + "/" + id);
                int result = contentResolver.delete(uri, null, null);
            }
        });

        allDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                contentResolver.delete(Words.Word.CONTENT_URI, null, null);
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = "7";
                String strWord = "customize";
                String strMeaning = "自定义";
                String strSample = "I want to customize a cup.";
                ContentValues values = new ContentValues();
                values.put(Words.Word.COLUMN_WORD, strWord);
                values.put(Words.Word.COLUMN_MEANING, strMeaning);
                values.put(Words.Word.COLUMN_SAMPLE, strSample);

                Uri uri = Uri.parse(Words.Word.CONTENT_URI_STRING + "/" + id);
                int result = contentResolver.update(uri, values, null, null);


            }
        });

        query.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                String id = "8";
                Uri uri = Uri.parse(Words.Word.CONTENT_URI_STRING + "/" + id);
                Cursor cursor = contentResolver.query(uri,
                        new String[]{Words.Word._ID, Words.Word.COLUMN_WORD,
                                Words.Word.COLUMN_MEANING, Words.Word.COLUMN_SAMPLE},
                        null, null, null);
                if (cursor == null) {
                    Toast.makeText(MainActivity.this, "没有找到记录", Toast.LENGTH_LONG).show();
                    return;
                }

                //找到记录
                String msg = "";
                if (cursor.moveToNext()) {
                    do {
                        msg += "ID:" + cursor.getInt(cursor.getColumnIndex(Words.Word._ID)) + ",";
                        msg += "单词：" + cursor.getString(cursor.getColumnIndex(Words.Word.COLUMN_WORD)) + ",";
                        msg += "含义：" + cursor.getInt(cursor.getColumnIndex(Words.Word.COLUMN_MEANING)) + ",";
                        msg += "例子：" + cursor.getFloat(cursor.getColumnIndex(Words.Word.COLUMN_SAMPLE)) + "\n";

                    } while (cursor.moveToNext());


                }

                Log.v(tag, msg);
            }
        });


    }
}